package ua.edu.sumdu.j2se.roman.lab1.view.swing;

import ua.edu.sumdu.j2se.roman.lab1.controller.Controller;
import ua.edu.sumdu.j2se.roman.lab1.domain.Task;
import ua.edu.sumdu.j2se.roman.lab1.util.Log;
import ua.edu.sumdu.j2se.roman.lab1.util.Utils;
import ua.edu.sumdu.j2se.roman.lab1.view.ReminderView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * Author: Radko Roman.
 */

/**
 * This class responsible for notification user about planned task with helps swing view.
 *
 */
public class SwingReminderView implements ReminderView {
    /**
     * Swing component that represents the user interface.
     *
     */
    private JFrame viewForm;
    /**
     * Contain description of planned task.
     *
     */
    private JLabel description ;

    /**
     * Constructor that call the method of initializing all components on view form.
     *
     */
    public SwingReminderView() {
        initView();
    }
    private Controller controller;
    /**
     * Initialize all components on view form.
     *
     */
    private void initView() {
        viewForm = new JFrame("Task reminder");
        viewForm.setSize(375, 200);
        viewForm.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        viewForm.setLocationRelativeTo(null);

        JButton postponeBtn = new JButton("Postpone");
        postponeBtn.setLocation(8, 30);
        postponeBtn.setSize(150, 35);
        postponeBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(viewForm, "Task postponed!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
                viewForm.setVisible(false);
                viewForm.dispose();
            }

        });
        viewForm.getContentPane().add(postponeBtn);

        JButton completeBtn = new JButton("Complete");
        completeBtn.setLocation(200, 30);
        completeBtn.setSize(150, 35);
        completeBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(viewForm, "Task completed!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
                viewForm.setVisible(false);
                viewForm.dispose();
            }

        });
        viewForm.getContentPane().add(completeBtn);


        description = new JLabel();
        description.setLocation(0, 0);
        description.setSize(300, 100);
        viewForm.getContentPane().add(description);
    }

    /**
     * Show to user reminder swing view form with description of planned task.
     *
     * @param task  planned task
     */
    @Override
    public void show(Task task) {
        Log.i("SwingReminderView with task: [" + task + "]");
        Utils.beep();
        updateUI(task);
        viewForm.setVisible(true);
        initView();
    }

    /**
     * Place at the view form text with task description.
     *
     * @param task  planned task
     */
    private void updateUI(Task task) {
        description = new JLabel(task.toString(),SwingConstants.CENTER);
        viewForm.getContentPane().add(description);
    }
}
